package com.cj.mobile.iabp.mvp.model;

import android.text.TextUtils;

import com.cj.mobile.iabp.app.global.AccountManager;
import com.cj.mobile.iabp.mvp.contract.AddRemoveInfoContract;
import com.cj.mobile.iabp.mvp.model.api.service.AccountService;
import com.jess.arms.cj.ApiOperator;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 2025/09/29 16:25
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class AddRemoveInfoModel extends BaseModel implements AddRemoveInfoContract.Model {
    @Inject
    ApiOperator apiOperator;                                                                        // 数据转换

    @Inject
    AccountManager mAccountManager;


    @Inject
    public AddRemoveInfoModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.apiOperator = null;
        this.mAccountManager = null;
    }

    @Override
    public Observable<String> getRunTime(String patienId, String time) {
        Map<String, Object> params = new HashMap<>();
        params.put("patienId", patienId);
        params.put("time", time);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).getRunTime(mAccountManager.getToken(), request));
    }

    @Override
    public Observable<List<String>> upload(List<String> files) {

        /*图片集*/
        List<MultipartBody.Part> parts = new ArrayList<>(files.size());
        for (String val : files) {
            File file = new File(val);
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
            parts.add(part);
        }

        //用户登录id
        RequestBody userIdVal = RequestBody.create(MediaType.parse("text/plain"), mAccountManager.getUserid());
        return mRepositoryManager.obtainRetrofitService(AccountService.class).uploadImages(mAccountManager.getToken(), userIdVal, parts).flatMap(ApiOperator.<List<String>>transformResponse());
    }

    @Override
    public Observable<Integer> addRemove(String per, String patienId, String doctorId, String deptId, String deptSec, String regCode,
                                         String patientName, String removeType, String removeTime, String runTimeStr, String focuse,
                                         String isAlive, String moreDiag, String moreDiagTxt, List<String> images) {
        Map<String, Object> params = new HashMap<>();
        params.put("per", per);
        params.put("patienId", patienId);
        params.put("doctorId", doctorId);
        params.put("deptId", deptId);
        params.put("deptSec", deptSec);
        params.put("regCode", regCode);
        params.put("patientName", patientName);

        params.put("removeType", removeType);
        params.put("removeTime", TextUtils.isEmpty(removeTime) ? null : removeTime);
        params.put("runTimeStr", runTimeStr);
        params.put("focuse", focuse);
        params.put("isAlive", isAlive);
        params.put("moreDiag", moreDiag);
        params.put("moreDiagTxt", moreDiagTxt);
        params.put("images", images);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).addRemove(mAccountManager.getToken(), request));
    }

    @Override
    public Observable<Integer> updateRemove(String per, String id, String patienId, String doctorId, String deptId, String deptSec, String regCode, String patientName, String removeType, String removeTime, String runTimeStr, String focuse, String isAlive, String moreDiag, String moreDiagTxt, List<String> images) {
        Map<String, Object> params = new HashMap<>();
        params.put("per", per);
        params.put("id", id);
        params.put("patienId", patienId);
        params.put("doctorId", doctorId);
        params.put("deptId", deptId);
        params.put("deptSec", deptSec);
        params.put("regCode", regCode);
        params.put("patientName", patientName);

        params.put("removeType", removeType);
        params.put("removeTime", TextUtils.isEmpty(removeTime) ? null : removeTime);
        params.put("runTimeStr", runTimeStr);
        params.put("focuse", focuse);
        params.put("isAlive", isAlive);
        params.put("moreDiag", moreDiag);
        params.put("moreDiagTxt", moreDiagTxt);
        params.put("images", images);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).updateRemove(mAccountManager.getToken(), request));
    }
}